package com.exonline.backstage.filter;

import com.exonline.commons.utils.SqlConditionBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;

@WebFilter("/admin/*")
public class MainFilter extends HttpFilter {
    ObjectMapper mapper = new ObjectMapper();
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {




        String origin = req.getHeader("Origin");
        String uri = req.getRequestURI();
        //基础resp跨域参数
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setHeader("Access-Control-Allow-Origin", origin);
//        resp.setHeader("Access-Control-Allow-Origin", "http://36.235.128.117:26398");
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With,Content-Type,Accept,Cookie,Authorization");
        resp.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        resp.setContentType("application/json");
        resp.setHeader("Content-Type", "application/json");


        if ("OPTIONS".equals(req.getMethod())) {
            resp.setStatus(HttpServletResponse.SC_OK);
            return;
        }


        System.out.println(req.getSession(false));
        System.out.println("在线用户数:"+req.getServletContext().getAttribute("onlineUsers"));
        System.out.println(SqlConditionBuilder.buildCourseCondition(req));
        System.out.println(uri);
        chain.doFilter(req, resp);

//        //放行拥有session和进行登录的请求
//        if(session!=null&&"/EXOnline/admin/login".equals(uri)){
//            chain.doFilter(req, resp);
//        }else {
//            req.getRequestDispatcher("/")
//        }
    }
}
